//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.cnooc.service;

import cn.cnooc.config.InfluxDbComUtil;
import cn.cnooc.mapper.mysql.MysqlMapper;
import cn.cnooc.config.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class WeatherService {
    @Autowired
    private MysqlMapper mysqlMapper;
    @Autowired
    private InfluxDbComUtil influxDbComUtil;

    private static final String TOTAL_GEN_POWER = "SELECT yc58 FROM tb_donggu_inverter1, tb_donggu_inverter2  order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String TOTAL_GEN_POWER_TODAY = "SELECT yc57 FROM tb_donggu_inverter1, tb_donggu_inverter2  order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String PHOTOVOLTAIC = "SELECT yc51,yc60 FROM tb_donggu_inverter1, tb_donggu_inverter2  order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String AIR_PUMP_IO_WATER_TEMP = "SELECT yc1,yc2,yc6,yc7 FROM tb_donggu_airheatpump1    order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String ES_INFO = "SELECT yx3,yx5,yx124,yx141,yc16,yc35 FROM tb_donggu_energystorage fill(0)  order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String METER = "SELECT yc10,yc12,yc13,yc15 FROM tb_donggu_smartmeter  order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String CENTER_PART = "SELECT yc34 as cdz,(yc35+yc37+yc38+yc39+yc40+yc41+yc44) as pump,yc36 as work FROM tb_donggu_smartswitch1 fill(0) order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String CENTER_PART2 = "SELECT (yc22+yc25+yc28) as cdz FROM tb_donggu_smartswitch2 fill(0) order by time desc limit 1  tz('Asia/Shanghai')";
    private static final String AC_LOAD = "SELECT yc33  FROM tb_donggu_smartswitch1 fill(0) order by time desc limit 1  tz('Asia/Shanghai')";

    public WeatherService() {
    }

    public ResultInfo getCapacity() {
        Map<String, Object> result = new HashMap();
        List<Map<String, Object>> totalGenPower = influxDbComUtil.query(TOTAL_GEN_POWER);
        List<Map<String, Object>> totalGenPowerToday = influxDbComUtil.query(TOTAL_GEN_POWER_TODAY);
        BigDecimal sumTotal = new BigDecimal(totalGenPower.get(0).get("yc58").toString()).add(new BigDecimal(totalGenPower.get(1).get("yc58").toString()));
        BigDecimal sumToday = new BigDecimal(totalGenPowerToday.get(0).get("yc57").toString()).add(new BigDecimal(totalGenPowerToday.get(1).get("yc57").toString()));
        result.put("sum", sumTotal);
        result.put("day", sumToday);
        result.put("co2sum", sumTotal.multiply(new BigDecimal("0.8843")));
        result.put("co2day", sumToday.multiply(new BigDecimal("0.8843")));
        result.put("wrong", true);
        return ResultInfo.success(result);
    }

    public ResultInfo getHotWater() {
        List<Map<String, Object>> ioWater = influxDbComUtil.query(AIR_PUMP_IO_WATER_TEMP);
        List<Map<String, Object>> esinfo = influxDbComUtil.query(ES_INFO);
        Map<String, Object> map = new HashMap();
        map.put("intcr", ioWater.get(0).get("yc6"));
        map.put("outtcr", ioWater.get(0).get("yc7"));
        map.put("soc", esinfo.get(0).get("yc35"));
        map.put("storedWrong",  esinfo.get(0));
        map.put("status", ioWater.get(0).get("yc2"));
        map.put("onoroff", ioWater.get(0).get("yc1"));
        return ResultInfo.success(map);
    }

    public ResultInfo getCombinedStatus() {
        return ResultInfo.success(influxDbComUtil.query(METER).get(0));
    }




    public ResultInfo getDeviceNumber() {
        List<Map<String, Object>> allDevice = this.mysqlMapper.findAllDevice();
        List<Map<String, Object>> list = influxDbComUtil.query(PHOTOVOLTAIC);
        Map<String, Object> map = new HashMap();
        map.put("50", list.get(0));
        map.put("100",  list.get(1));
        map.put("device", allDevice);
        return ResultInfo.success(map);
    }

    public ResultInfo getWiringDiagram() {
        Map<String, Object> map = new HashMap();
        List<Map<String, Object>> list = influxDbComUtil.query(PHOTOVOLTAIC);
        List<Map<String, Object>> es = influxDbComUtil.query(ES_INFO);
        List<Map<String, Object>> cp = influxDbComUtil.query(CENTER_PART);
        List<Map<String, Object>> cp2 = influxDbComUtil.query(CENTER_PART2);
        List<Map<String, Object>> acLoad = influxDbComUtil.query(AC_LOAD);

        map.put("kw50", list.get(1).get("yc51"));
        map.put("kw100", list.get(0).get("yc51"));
        map.put("stored", es.get(0).get("yc16"));
        map.put("chargingPile", new BigDecimal(cp.get(0).get("cdz").toString()).add(new BigDecimal(cp2.get(0).get("cdz").toString())));
        map.put("heatPump", cp.get(0).get("pump"));
        map.put("work", cp.get(0).get("work"));
        map.put("dc", 0);
        map.put("photovoltaicArray", 0);
        map.put("ACLoad", acLoad.get(0).get("yc33"));
        return ResultInfo.success(map);
    }


}
